<%@ page pageEncoding="UTF-8"%>
<%@ page import="java.util.Map.Entry,java.util.*,java.util.regex.*,java.net.*,java.io.*,java.text.*"%>
<%@ page import="com.tianxiabuyi.*,com.tianxiabuyi.db.*,com.tianxiabuyi.model.*,com.tianxiabuyi.util.*"%>
<%@ page import="org.jsoup.Jsoup,org.jsoup.nodes.Element,org.jsoup.select.Elements"%>
<%@ page import="com.google.gson.reflect.TypeToken"%>
<%@ page import="org.dom4j.*"%>
<%@ include file = "user.jsp"%>
<%@ include file = "operatehistory.jsp"%>

<%!
	/**********************去除特殊字符的方法*************************/
	String getCheck(String str){
		str = str.replaceAll("\\["," [ ");
		str = str.replaceAll("\\]"," ] ");
		str = str.replaceAll("\"", " “ ");

		return str;
	}
	// 问题列表
	String quests(Input input) {
		return search(input);
	}
	
	

	// 我的问题列表
	String my(Integer uid, Input input) {
		input.setDesc("uid", uid + "");
		input.setDesc("query", "my");
		return search(input);
	}

	// 搜索问题列表
	String search(Input input) {
		Integer page = 1;
		Integer itemCount = 20;
		Integer groupId = input.getInt("group", -1); // 分组id, 默认全部分类问题
		if(!input.getDesc("group").equals("暂无")){
			groupId =Integer.parseInt(input.getDesc("group"));
		}
		Integer uuid = input.getInt("uid", -11); // 用户id
		String group = groupId == -1?"":("groupId=" + groupId);
		Integer hospital = input.getInt("hospital", -1); // 医院id
		String sinceID = input.getDesc("since_id","0"); // 下拉刷新
		String maxID = input.getDesc("max_id","0"); // 上拉加载
		String content = input.getDesc("content", ""); // 上拉加载
		
		Integer type = input.getInt("type", -1); // 问答1  社区2
		
		if (!"".equals(content)) {
			content = content.replace("/","//").replace("\\","/\\\\").replace("_","/_").replace("%","/%");
			content = " title like '%" + content + "%' escape '/' or content like '%" + content + "%' escape '/'";
		}
		String query = input.getDesc("query", "");
		if ("my".equals(query)) {
			query = "uid=" + uuid;
		} else if ("answered".equals(query) || "to_answer".equals(query)) {
			query = "aid=" + uuid;
		}
		String order = "desc"; // 排序
		String questId = "";
		String selectContent = "new map(id as id, title as title,case when imgs is null then '[]' else imgs end as imgs, uid as uid, content as content, cTime as create_time, aid as aid, groupId as group)";
		if (!sinceID.equals("0")) {
			questId = "id > " + sinceID;
			order = "asc";
		} else if (!maxID.equals("0")) {
			questId = "id < " + maxID;
		}
		List<Map> groupList = null;
		
		if(hospital != 1068){
			groupList = QBuilder.db("basedb").select("new map(id as id, name as name, type as type)").from("QuestGroup").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE)).query().getResultObj();
		}else{
			
			if(groupId == 37){
				type = 3;
				groupList = QBuilder.db("basedb").select("new map(id as id, name as name, type as type)").from("QuestGroup").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("type=", "3")).query().getResultObj();
			}else{
				
				String hql = "select new map(id as id, name as name, type as type)  from QuestGroup where  status="+STATUS_ENABLE +" and hospital="+hospital +" and (type = 1 or type =2)";
				groupList = QBuilder.db("basedb").queryHql(hql);
			}
			
		}
		
		if(query.indexOf("uid") == -1){// 非 my
			if (type != -1) {
				String groupIdStr = "(";
				for (Map groupMap:groupList) {
					if ((Integer)groupMap.get("type") == type ) {
						groupIdStr = groupIdStr + groupMap.get("id") + ",";
					}	
				}
				if (groupIdStr.length() == 1) {
					group = "groupId in (-1)"  ;
				} else {
					groupIdStr = groupIdStr.substring(0, groupIdStr.length()-1) + ")";
					
					group = "groupId in" + groupIdStr;
				}
				
			}
			
		}else{
			if (type == 3) {
				String groupIdStr = "(";
				for (Map groupMap:groupList) {
					
					groupIdStr = groupIdStr + groupMap.get("id") + ",";
									
				}
				if (groupIdStr.length() == 1) {
					group = "groupId in (-1)"  ;
				} else {
					groupIdStr = groupIdStr.substring(0, groupIdStr.length()-1) + ")";
					group = "groupId in" + groupIdStr;
				}
			}else{
				String groupIdStr = "(";
				for (Map groupMap:groupList) {
					
						groupIdStr = groupIdStr + groupMap.get("id") + ",";
					
				}
				if (groupIdStr.length() == 1) {
					group = "groupId in (-1)"  ;
				} else {
					groupIdStr = groupIdStr.substring(0, groupIdStr.length()-1) + ")";
					if(hospital == 1072){
						groupIdStr = groupIdStr.substring(0, groupIdStr.length()-1) + ",-1)";
					}
				
					
					group = "groupId in" + groupIdStr;
				}
				
			}
		}
		
		
		
		
		
	
		
	
		List<Map> questList = QBuilder.db("basedb").select(selectContent).from("Quest").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE).and(questId).and(group).and(content).and(query)).order("id", order).page(itemCount, page).query().getResultObj();
		for(Map ma : questList) {

			if (ma.get("content") != null) {
				String content1 = ma.get("content").toString();
				/*****************特殊字符处理***********************/
				content1 = getCheck(content1);
				ma.put("content", content1);
			}
			//标题特殊字符处理
			if (ma.get("title") != null) {
				String title = ma.get("title").toString();
				/*****************特殊字符处理***********************/
				title = getCheck(title);
				ma.put("title", title);
			}
		}
		if (order.equals("asc")) { // 上拉加载时倒序
			Collections.reverse(questList);
		}
		// 评论 category 1为问答 2为新闻
		List<Map> commentList = QBuilder.db("basedb").queryHql("select new map(count(*) as count, srcId as src_id) from Comment where hospital=" + hospital + " and category=" + 1 + " group by srcId");

		List<Integer> uidList = new ArrayList<Integer>();
		for (int i = 0; i < questList.size(); i++) {
			Map quest = (Map)questList.get(i);
			Integer uid = Tools.getInt(quest.get("uid") + "", 0);
			Long createTime = Tools.getLong(quest.get("create_time") + "");
			String create_time = DateUtil.formatDateByFormat(new Date(createTime), DateUtil.DATETIME_FORMAT);
			quest.put("create_time", create_time);
			uidList.add(uid);
		}
		List<Integer> topList = QBuilder.db("basedb").select("oid").from("Top").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("category=", 1)).query().getResultObj();
		// 查询用户信息
		Map<Integer, User> userMap = usersByUids(uidList);
		for (int i = 0; i < questList.size(); i++) {
			Map quest = (Map)questList.get(i);
			Integer uid = Tools.getInt(quest.get("uid") + "", 0);
			quest.remove("uid");
			User user = userMap.get(uid);
			if (user != null) {
				String userName = user.getUserName();
				String nickName = user.getNickName();
				String avatar = user.getAvatar();
				String name = user.getName();
				quest.put("user_name", ((nickName!= null) && (nickName.length() > 0)) ? nickName : userName);
				quest.put("avatar", avatar);
				quest.put("name", name);
			}
			quest.put("comment", 0);
			for (Map commentMap:commentList) {
				if (((Integer)quest.get("id")).equals((Integer)commentMap.get("src_id"))) {
					quest.put("comment", commentMap.get("count"));
				}
			}
			quest.put("group_name", "");
			for (Map groupMap:groupList) {
				if (((Integer)quest.get("group")).equals((Integer)groupMap.get("id"))) {
					quest.put("group_name", groupMap.get("name"));
				}
			}
			quest.put("top", topList.contains((Integer)quest.get("id"))?1:0);
		}
		List<Integer> myComment = QBuilder.db("basedb").select("srcId").from("Comment").where(new Where("hospital=", hospital).and("category=", 1).and("uid=", uuid).and("status=", STATUS_ENABLE)).query().getResultObj();
		Iterator<Map> questIter = questList.iterator();
		if ("answered".equals(input.getDesc("query", ""))) {
			while (questIter.hasNext()) {
				Map questMap = questIter.next();
				Integer id = (Integer)questMap.get("id");
				if (!myComment.contains(id))  {
					questIter.remove();
				}
			}
		}
		if ("to_answer".equals(input.getDesc("query", ""))) {
			while (questIter.hasNext()) {
				Map questMap = questIter.next();
				Integer id = (Integer)questMap.get("id");
				if (myComment.contains(id)) {
					questIter.remove();
				}
			}
		}
        Message msg = new Message(0);
		OperateHistory oh = new OperateHistory();
		oh.setHospital(hospital);
		oh.setCategory(QUEST_CATEGORY);
		oh.setOperate(LOVE);
        msg.put("quests", love(query(questList, oh), uuid, oh));
        return msg.toJson();
	}

	// 医生查询该患者下的所有信息
	String searchMyPatient(Input input) {
		Integer page = 1;
		Integer itemCount = 20;
		Integer groupId = input.getInt("group", -1); // 分组id, 默认全部分类问题
		if(!input.getDesc("group").equals("暂无")){
			groupId =Integer.parseInt(input.getDesc("group"));
		}
		Integer uuid = Integer.parseInt(input.getDesc("uuid")); // 用户id
		Integer aid = Integer.parseInt(input.getDesc("aid")); // 医生 id
		String group = groupId == -1?"":("groupId=" + groupId);
		Integer hospital = input.getInt("hospital", -1); // 医院id
		String sinceID = input.getDesc("since_id","0"); // 下拉刷新
		String maxID = input.getDesc("max_id","0"); // 上拉加载
		String content = input.getDesc("content", ""); // 上拉加载
		
		
		List<Expert> expertList = QBuilder.db("basedb").queryHql("from Expert where  userId ="+ aid +"" );
		
		aid = expertList.get(0).getId();
		
	
		
		String order = "desc"; // 排序
		
		String questId = "";
		String selectContent = "new map(id as id, title as title,case when imgs is null then '[]' else imgs end as imgs, uid as uid, content as content, cTime as create_time, aid as aid, groupId as group)";
		
		List<Map> groupList = null;
		
		groupList = QBuilder.db("basedb").select("new map(id as id, name as name, type as type)").from("QuestGroup").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("id=", "37")).query().getResultObj();
		
		List<Map> questList = QBuilder.db("basedb").select(selectContent).from("Quest").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE).and("uid=",uuid).and("aid=",aid)).order("id", order).page(itemCount, page).query().getResultObj();
		
		
		if (order.equals("asc")) { // 上拉加载时倒序
			Collections.reverse(questList);
		}
		// 评论 category 1为问答 2为新闻
		List<Map> commentList = QBuilder.db("basedb").queryHql("select new map(count(*) as count, srcId as src_id) from Comment where hospital=" + hospital + " and category=" + 1 + " group by srcId");

		List<Integer> uidList = new ArrayList<Integer>();
		for (int i = 0; i < questList.size(); i++) {
			Map quest = (Map)questList.get(i);
			Integer uid = Tools.getInt(quest.get("uid") + "", 0);
			Long createTime = Tools.getLong(quest.get("create_time") + "");
			String create_time = DateUtil.formatDateByFormat(new Date(createTime), DateUtil.DATETIME_FORMAT);
			quest.put("create_time", create_time);
			uidList.add(uid);
		}
		List<Integer> topList = QBuilder.db("basedb").select("oid").from("Top").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("category=", 1)).query().getResultObj();
		// 查询用户信息
		Map<Integer, User> userMap = usersByUids(uidList);
		for (int i = 0; i < questList.size(); i++) {
			Map quest = (Map)questList.get(i);
			Integer uid = Tools.getInt(quest.get("uid") + "", 0);
			quest.remove("uid");
			User user = userMap.get(uid);
			if (user != null) {
				String userName = user.getUserName();
				String nickName = user.getNickName();
				String avatar = user.getAvatar();
				String name = user.getName();
				quest.put("user_name", ((nickName!= null) && (nickName.length() > 0)) ? nickName : userName);
				quest.put("avatar", avatar);
				quest.put("name", name);
			}
			quest.put("comment", 0);
			for (Map commentMap:commentList) {
				if (((Integer)quest.get("id")).equals((Integer)commentMap.get("src_id"))) {
					quest.put("comment", commentMap.get("count"));
				}
			}
			quest.put("group_name", "");
			for (Map groupMap:groupList) {
				if (((Integer)quest.get("group")).equals((Integer)groupMap.get("id"))) {
					quest.put("group_name", groupMap.get("name"));
				}
			}
			quest.put("top", topList.contains((Integer)quest.get("id"))?1:0);
		}
		List<Integer> myComment = QBuilder.db("basedb").select("srcId").from("Comment").where(new Where("hospital=", hospital).and("category=", 1).and("uid=", uuid).and("status=", STATUS_ENABLE)).query().getResultObj();
		Iterator<Map> questIter = questList.iterator();
		if ("answered".equals(input.getDesc("query", ""))) {
			while (questIter.hasNext()) {
				Map questMap = questIter.next();
				Integer id = (Integer)questMap.get("id");
				if (!myComment.contains(id))  {
					questIter.remove();
				}
			}
		}
		if ("to_answer".equals(input.getDesc("query", ""))) {
			while (questIter.hasNext()) {
				Map questMap = questIter.next();
				Integer id = (Integer)questMap.get("id");
				if (myComment.contains(id)) {
					questIter.remove();
				}
			}
		}
        Message msg = new Message(0);
		OperateHistory oh = new OperateHistory();
		oh.setHospital(hospital);
		oh.setCategory(QUEST_CATEGORY);
		oh.setOperate(LOVE);
        msg.put("quests", love(query(questList, oh), uuid, oh));
		

		
        return msg.toJson();
	}
	
	// 问题详情
	String show(Input input) {
		Message msg = new Message(0);
		Integer questId = input.getInt("quest_id", 0);
		Integer hospital = input.getInt("hospital", -1); // 医院id
		Integer uuid = input.getInt("uid", -1); // 用户id
		List<Map> commentList = QBuilder.db("basedb").select("new map(id as id,case when imgs is null then '[]' else imgs end as imgs, uid as uid, content as content, cTime as create_time)").from("Comment").where(new Where("srcId=", questId).and("hospital=", hospital).and("category=", 1).and("status=", STATUS_ENABLE)).order("id", "asc").query().getResultObj();

		for(Map<String,Object> m : commentList){
			String content = m.get("content").toString();
			content = getCheck(content);
			m.put("content",content);
		}
		List<Integer> uidList = new ArrayList<Integer>();
		for (int i = 0; i < commentList.size(); i++) {
			Map comment = commentList.get(i);
			Integer uid = Tools.getInt(comment.get("uid") + "", 0);
			Long createTime = Tools.getLong(comment.get("create_time") + "");
			String create_time = DateUtil.formatDateByFormat(new Date(createTime), DateUtil.DATETIME_FORMAT);
			comment.put("create_time", create_time);
			uidList.add(uid);
		}
		Map<Integer, User> userMap = usersByUids(uidList);
		for (int i = 0; i < commentList.size(); i++) {
			Map comment = (Map)commentList.get(i);
			Integer uid = Tools.getInt(comment.get("uid") + "", 0);
			comment.remove("uid");
			User user = userMap.get(uid);
			if (user != null) {
				String userName = user.getUserName();
				String nickName = user.getNickName();
				String avatar = user.getAvatar();
				String name = user.getName();
				comment.put("user_name", ((nickName!= null) && (nickName.length() > 0)) ? nickName : userName);
				comment.put("name", name);
				comment.put("avatar", avatar);
				comment.put("type", user.getType());  // 用于判断回答用户的角色
			}
		}
		OperateHistory oh = new OperateHistory();
		oh.setHospital(hospital);
		oh.setCategory(COMMENT_CATEGORY);
		oh.setOperate(LOVE);
		msg.put("replies", love(query(commentList, oh), uuid, oh));
		oh.setCategory(QUEST_CATEGORY);
		oh.setUid(uuid+"");
		oh.setOid(questId+"");
		oh.setOperate(SEE);
		operate(oh, false);
		String selectContent = "new map(id as id, title as title,case when imgs is null then '[]' else imgs end as imgs, uid as uid, content as content, cTime as create_time, aid as aid, groupId as group)";
		List<Map> questList = QBuilder.db("basedb").select(selectContent).from("Quest").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE).and("id=", questId)).query().getResultObj();
		if (questList.size() == 0) {
			setErrcode(msg, NO_RESULT);
			return msg.toJson();
		}

		for(Map<String,Object> m : questList){
			String content = m.get("content").toString();
			content = getCheck(content);
			m.put("content",content);

		}
		List<Map> groupList = QBuilder.db("basedb").select("new map(id as id, name as name)").from("QuestGroup").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE)).query().getResultObj();
		List<Integer> questUidList = new ArrayList<Integer>();
		for (int i = 0; i < questList.size(); i++) {
			Map quest = (Map)questList.get(i);
			Integer uid = Tools.getInt(quest.get("uid") + "", 0);
			Long createTime = Tools.getLong(quest.get("create_time") + "");
			String create_time = DateUtil.formatDateByFormat(new Date(createTime), DateUtil.DATETIME_FORMAT);
			quest.put("create_time", create_time);
			questUidList.add(uid);
		}
		List<Integer> topList = QBuilder.db("basedb").select("oid").from("Top").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("category=", 1)).query().getResultObj();
		// 查询用户信息
		Map<Integer, User> questUserMap = usersByUids(questUidList);
		for (int i = 0; i < questList.size(); i++) {
			Map quest = (Map)questList.get(i);
			Integer uid = Tools.getInt(quest.get("uid") + "", 0);
			quest.remove("uid");
			User user = questUserMap.get(uid);
			if (user != null) {
				String userName = user.getUserName();
				String avatar = user.getAvatar();
				String name = user.getName();
				quest.put("user_name", userName);
				quest.put("avatar", avatar);
				quest.put("name", name);
			}
			quest.put("comment", 0);
			for (Map commentMap:commentList) {
				if (((Integer)quest.get("id")).equals((Integer)commentMap.get("src_id"))) {
					quest.put("comment", commentMap.get("count"));
				}
			}
			quest.put("group_name", "");
			for (Map groupMap:groupList) {
				if (((Integer)quest.get("group")).equals((Integer)groupMap.get("id"))) {
					quest.put("group_name", groupMap.get("name"));
				}
			}
			quest.put("top", topList.contains((Integer)quest.get("id"))?1:0);
		}
		OperateHistory ohQuest = new OperateHistory();
		ohQuest.setHospital(hospital);
		ohQuest.setCategory(QUEST_CATEGORY);
		ohQuest.setOperate(LOVE);
        msg.put("quest", love(query(questList, ohQuest), uuid, ohQuest).get(0));
        return msg.toJson();
	}

	// 提问
	String create(Integer uid, Input input) {
		Message msg = new Message(0);
		String title = input.getDesc("title", "");
		String content = input.getDesc("content", "");
		Integer aid = input.getInt("aid", -1);
		Integer gid = input.getInt("group", -1);
		if (title.length() < 3) {
			setErrcode(msg, LENGTH_ILLEGAL);
			return msg.toJson();
		}
		if (content.length() < 3) {
			setErrcode(msg, LENGTH_ILLEGAL);
			return msg.toJson();
		}
		Quest quest = new Quest();
		Integer hospital = input.getInt("hospital", -1);
		quest.setHospital(hospital);
		quest.setUid(uid);
		quest.setTitle(title);
		quest.setContent(content);
		quest.setAid(aid);
		quest.setGroupId(gid);
		String imgs = input.getDesc("imgs", Tools.encryptStr("[]"));
		quest.setImgs(Tools.decryptStr(imgs));
		Integer id = (Integer)DBTools.db("basedb").addItem(quest);
		if (hospital == 1068) {
			
			if(gid == 37){//收费提问
					String hql = "from Transaction where hospital ='" + hospital + "' and hisToken ='" + aid+"' and payUid ="+ uid +" order by cTime desc";
					System.out.println("hql:"+hql);
					List<Transaction> tcList = QBuilder.db("basedb").queryHql(hql);
					System.out.println("tcList:"+tcList.size());
					Transaction tsct = tcList.get(0);
					Map<String, String> docOrigMap =  GsonUtil.fromJson(tsct.getJsonString(), new TypeToken<Map<String, String>>() { });//转换json
					double cou = Double.parseDouble(docOrigMap.get("count"));
					int countm = (int)cou;
					docOrigMap.put("count",(countm-1)+"");
					
					int res = QBuilder.db("basedb").execHQL("update Transaction set jsonString='"+GsonUtil.toJson(docOrigMap)+"' where id =" + tsct.getId() );
					
					
					List<Expert> expertList = QBuilder.db("basedb").queryHql("from Expert where  id ="+ aid +"" );
					
					System.out.println("expertList:"+expertList.size());
					aid = expertList.get(0).getUserId();
			}
			// 社区提问不减分
			if((gid == 2) || (gid == 3) || (gid == 4)) {
				Map scoreMap = new HashMap();
				scoreMap.put("hospital", hospital);
				scoreMap.put("uid", uid);
				scoreMap.put("status", STATUS_ENABLE);
				scoreMap.put("json", "{}");
				scoreMap.put("create_time", System.currentTimeMillis());
				scoreMap.put("activity_id", id);
				scoreMap.put("operator", uid + "");
				scoreMap.put("category", QUEST_CATEGORY);
				scoreMap.put("score", -5);
				DBTools.db("basedb").addItem("user_score", scoreMap);
			}
			final Integer uuid = uid;
			System.out.println("aid:"+aid);
			if (aid!= -1) {
				List<Integer> uList = new ArrayList<Integer>();
				uList.add(uid);
				Map<Integer, User> userMap = usersByUids(uList);
				String name = userMap.get(uuid).getUserName();
				Map param = new HashMap();
				param.put("hospital", hospital);
				param.put("app_type", input.getDesc("app_type", "section"));
				param.put("title", name + "给你提了一个问题");
				param.put("content", title);
				param.put("id", id);
				param.put("sender", name);
				param.put("uids", Tools.encryptStr("[\"" + aid + "\"]"));
				if(gid == 37){//收费提问
					param.put("category", "-3");
				}
				String basePath = input.getDesc("basePath");
				HttpUtil.encryptPost((basePath+"/v2/push/push"), param);
				
			}
		}
		return msg.toJson();
	}

	// 回复
	String reply(Integer uid, Input input) {
		Message msg = new Message(0);
		Integer questId = input.getInt("quest_id", 0);
		String content = input.getDesc("content", "");
		Integer hospital = input.getInt("hospital", -1);
		if (content.length() < 3) {
			setErrcode(msg, LENGTH_ILLEGAL);
			return msg.toJson();
		}
		List<Quest> questList = QBuilder.db("basedb").queryHql("from Quest where id=" + questId);
		if (questList.size() == 0) {
			setErrcode(msg, NO_RESULT);
			return msg.toJson();
		}
		com.tianxiabuyi.model.Comment comment = new com.tianxiabuyi.model.Comment();
		comment.setHospital(hospital);
		comment.setUid(uid);
		comment.setSrcId(questId);
		comment.setContent(content);
		comment.setCategory(1);
		String imgs = input.getDesc("imgs", Tools.encryptStr("[]"));
		comment.setImgs(Tools.decryptStr(imgs));
		DBTools.db("basedb").addItem(comment);
		if (hospital == 1068 || hospital == 1104) {
			List<Integer> receviveList = QBuilder.db("basedb").select("uid").from("Quest").where(new Where("hospital=", hospital).and("id=", questId)).query().getResultObj();
			final Integer uuid = uid;
			Map<Integer, User> userMap = usersByUids(new ArrayList<Integer>(){{add(uuid);}});
			String name = userMap.get(uuid).getUserName();
			Map param = new HashMap();
			param.put("hospital", hospital);
			param.put("app_type", input.getDesc("app_type", "section"));
			param.put("title", name + "回答了你的问题");
			param.put("content", content);
			param.put("id", questId);
			param.put("sender", name);
			if(questList.get(0).getGroupId() == 37){//收费回复
				param.put("category", "-4");
			}
			Integer rid = receviveList.size() == 0?0:receviveList.get(0);
			
			if (!uid.equals(rid)) {
				param.put("uids", Tools.encryptStr("[\"" +rid  + "\"]"));
				String basePath = input.getDesc("basePath");
				
				HttpUtil.encryptPost((basePath+"/v2/push/push"), param);
			} 
		}
		return show(input);
	}

	// 分组
	String categorys(Input input) {
		Message msg = new Message(0);
		Integer hospital = input.getInt("hospital", -1);
		Integer typeNum = input.getInt("type", -1);
		String type = "";
		if (-1 != typeNum) {
			type = "type=" + typeNum;
		}
		List<Map<String, Object>> groupList = QBuilder.db("basedb").select("new map(id as id, name as name)").from("QuestGroup").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and(type)).order("order", "asc").query().getResultObj();
		if (hospital == 1108) { // 苏州市立医院本部急危重症科
			for (Map<String, Object> map : groupList) {
				String name = Tools.getDesc(map.get("name"), "");
				if ("ICU".equals(name)) {
					map.put("content", "献出你的爱，生命更精彩");
				} else if ("成人急诊".equals(name)) {
					map.put("content", "急在分秒之间，救在生死边缘");
				} else if ("儿童急诊".equals(name)) {
					map.put("content", "解读家长疑问，关注儿童健康");
				} else if ("急诊内科".equals(name)) {
					map.put("content", "病人至上，人道奉献");
				} else {
					map.put("content", "关爱生命，尊重生命");
				}
			}
		}
		msg.put("categorys", groupList);
		return msg.toJson();
	}

	// 展示赞或踩详情
	String showLove(Input input) {
		Message msg = new Message(0);
		Integer id = input.getInt("id");
		Integer type = input.getInt("type", LOVE);
		List<Map> list = QBuilder.db("basedb").select("new map(u.userName as user_name,u.name as name, u.avatar as avatar)").from("OperateHistory as oh, User as u").where(new Where("oh.oid=", id).and("oh.category=", QUEST_CATEGORY).and("oh.status=", STATUS_ENABLE).and("oh.uid=u.id").and("oh.operate=", type)).query().getResultObj();
		msg.put("love", list);
		return msg.toJson();
	}

	// 热门帖子
	String hot(Input input) {
		Integer page = input.getInt("page", 1);
		Integer itemCount = 20;
		Integer uuid = input.getInt("uid", -11); // 用户id
		Integer hospital = input.getInt("hospital", -1); // 医院id
		String date = input.getDesc("date", ""); // 医院id
		String peroid = "";
		String count = "";
		if (! "".equals(date)) {
			Map<String, Long> dateMap = date(date);
			peroid = " and q.cTime >= " + dateMap.get("start_time") + " and q.cTime < " + dateMap.get("end_time");
			count = "cTime >= " + dateMap.get("start_time") + " and cTime < " + dateMap.get("end_time");
		}
		List<Map> questList = QBuilder.db("basedb").queryHql("select new map(q.id as id, q.title as title,case when q.imgs is null then '[]' else q.imgs end as imgs, q.uid as uid, q.content as content, q.cTime as create_time, q.aid as aid, q.groupId as group,count(c.srcId) as comment) from Quest as q ,Comment as c where q.id=c.srcId and q.hospital=" + hospital +" and q.status = " + STATUS_ENABLE + peroid + " and c.category=" + 1 + " group by c.srcId order by count(c.srcId) desc ", itemCount, page);
		List<Long> questCount = QBuilder.db("basedb").select("count(*)").from("Quest").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and(count)).query().getResultObj();
		List<Map> groupList = QBuilder.db("basedb").select("new map(id as id, name as name)").from("QuestGroup").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE)).query().getResultObj();
		List<Integer> uidList = new ArrayList<Integer>();
		for (int i = 0; i < questList.size(); i++) {
			Map quest = (Map)questList.get(i);
			Integer uid = Tools.getInt(quest.get("uid") + "", 0);
			Long createTime = Tools.getLong(quest.get("create_time") + "");
			String create_time = DateUtil.formatDateByFormat(new Date(createTime), DateUtil.DATETIME_FORMAT);
			quest.put("create_time", create_time);
			uidList.add(uid);
		}

		// 查询用户信息
		Map<Integer, User> userMap = usersByUids(uidList);
		for (int i = 0; i < questList.size(); i++) {
			Map quest = (Map)questList.get(i);
			Integer uid = Tools.getInt(quest.get("uid") + "", 0);
			quest.remove("uid");
			User user = userMap.get(uid);
			if (user != null) {
				String userName = user.getUserName();
				String avatar = user.getAvatar();
				String name = user.getName();
				quest.put("user_name", userName);
				quest.put("avatar", avatar);
				quest.put("name", name);
			}
			quest.put("group_name", "");
			for (Map groupMap:groupList) {
				if (((Integer)quest.get("group")).equals((Integer)groupMap.get("id"))) {
					quest.put("group_name", groupMap.get("name"));
				}
			}
		}
        Message msg = new Message(0);
		OperateHistory oh = new OperateHistory();
		oh.setHospital(hospital);
		oh.setCategory(QUEST_CATEGORY);
		oh.setOperate(LOVE);
        msg.put("quests", love(query(questList, oh), uuid, oh));
		msg.put("page", page);
		Long pageCount = questCount.get(0)%itemCount == 0?(questCount.get(0)/itemCount):(questCount.get(0)/itemCount + 1);
		msg.put("page_count", pageCount);
        return msg.toJson();
	}

	// 置顶
	public String top(Input input) {
		Integer group = input.getInt("group");
		Integer hospital = input.getInt("hospital");
		Integer uuid = input.getInt("uid");
		List<Map> questList = QBuilder.db("basedb").queryHql("select new map(q.id as id, q.title as title,case when q.imgs is null then '[]' else q.imgs end as imgs, q.uid as uid, q.content as content, q.cTime as create_time, q.aid as aid, q.groupId as group) from Quest as q ,Top as t where q.id=t.oid and q.hospital=" + hospital +" and q.groupId= " + group +" and q.status = " + STATUS_ENABLE + "  order by t.id desc ", 3, 1);
		 Message msg = new Message(0);
		OperateHistory oh = new OperateHistory();
		oh.setHospital(hospital);
		oh.setCategory(QUEST_CATEGORY);
		oh.setOperate(LOVE);
        msg.put("quests", love(query(questList, oh), uuid, oh));
		return msg.toJson();
	}

	// 已经回答
	String answered(Integer uuid, Input input) {
		Integer page = 1;
		Integer itemCount = 20;
		Integer hospital = input.getInt("hospital", -1); // 医院id
		String sinceID = input.getDesc("since_id","0"); // 下拉刷新
		String maxID = input.getDesc("max_id","0"); // 上拉加载
		String order = "desc"; // 排序
		String questId = "";
		String selectContent = "distinct new map(q.id as id, q.title as title,case when q.imgs is null then '[]' else q.imgs end as imgs, q.uid as uid, q.content as content, q.cTime as create_time, q.aid as aid, q.groupId as group)";
		if (!sinceID.equals("0")) {
			questId = "q.id > " + sinceID;
			order = "asc";
		} else if (!maxID.equals("0")) {
			questId = "q.id < " + maxID;
		}
		
		
		//查询用户信息
		String hql = "from QuestGroup where hospital ='" + hospital + "' and type = 3 ";
		//获取个人缴费的所有记录 
		List<QuestGroup> chList = QBuilder.db("basedb").queryHql(hql);
		
		Integer groupId = 0;
		if(chList != null && chList.size()>0){
			groupId = chList.get(0).getId();
		}
		
	String hql1 = "q.hospital= " + hospital +" and  q.status = "+STATUS_ENABLE+questId+" and q.id=c.srcId"+" and c.status= "+STATUS_ENABLE+" and   c.uid="+uuid+"  and  c.category=1 " + " and q.groupId!= "+groupId;
		System.out.println("groupId:"+groupId);
		List<Map> questList = QBuilder.db("basedb").select(selectContent).from("Quest as q, Comment as c").where(hql1).order("q.id", order).page(itemCount, page).query().getResultObj();
		if (order.equals("asc")) { // 上拉加载时倒序
			Collections.reverse(questList);
		}
		// 评论 category 1为问答 2为新闻
		List<Map> commentList = QBuilder.db("basedb").queryHql("select new map(count(*) as count, srcId as src_id) from Comment where hospital=" + hospital + " and category=" + 1 + " group by srcId");
		List<Map> groupList = QBuilder.db("basedb").select("new map(id as id, name as name)").from("QuestGroup").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE)).query().getResultObj();
		List<Integer> uidList = new ArrayList<Integer>();
		for (int i = 0; i < questList.size(); i++) {
			Map quest = (Map)questList.get(i);
			Integer uid = Tools.getInt(quest.get("uid") + "", 0);
			Long createTime = Tools.getLong(quest.get("create_time") + "");
			String create_time = DateUtil.formatDateByFormat(new Date(createTime), DateUtil.DATETIME_FORMAT);
			quest.put("create_time", create_time);
			uidList.add(uid);
		}
		// 查询用户信息
		Map<Integer, User> userMap = usersByUids(uidList);
		for (int i = 0; i < questList.size(); i++) {
			Map quest = (Map)questList.get(i);
			Integer uid = Tools.getInt(quest.get("uid") + "", 0);
			quest.remove("uid");
			User user = userMap.get(uid);
			if (user != null) {
				String userName = user.getUserName();
				String avatar = user.getAvatar();
				String name = user.getName();
				quest.put("user_name", userName);
				quest.put("avatar", avatar);
				quest.put("name", name);
			}
			quest.put("comment", 0);
			for (Map commentMap:commentList) {
				if (((Integer)quest.get("id")).equals((Integer)commentMap.get("src_id"))) {
					quest.put("comment", commentMap.get("count"));
				}
			}
			quest.put("group_name", "");
			for (Map groupMap:groupList) {
				if (((Integer)quest.get("group")).equals((Integer)groupMap.get("id"))) {
					quest.put("group_name", groupMap.get("name"));
				}
			}
		}
        Message msg = new Message(0);
		OperateHistory oh = new OperateHistory();
		oh.setHospital(hospital);
		oh.setCategory(QUEST_CATEGORY);
		oh.setOperate(LOVE);
        msg.put("quests", love(query(questList, oh), uuid, oh));
        return msg.toJson();
	}

	// 待回答
	String toAnswer(Integer uid, Input input) {
		input.setDesc("query", "to_answer");
		input.setDesc("uid", uid + "");
		return search(input);
	}

	 Map<String, Long> date(String dateStr) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(DateUtil.parseDate(dateStr));
		int month = cal.get(Calendar.MONTH) + 1;
		int year = cal.get(Calendar.YEAR);
		String start = month + "";
		String end = (month + 1) + "";
		if (start.length() == 1) {
			start = "0" + start;
		}
		if (end.length() == 1) {
			end = "0" + end;
		}
		start = year + "-" + start + "-01";
		end = year + "-" + end + "-01";
		Date startTime = DateUtil.parseDate(start);
		Date endTime = DateUtil.parseDate(end);
		Map<String, Long> map = new HashMap<String, Long>();
		map.put("start_time", startTime.getTime());
		map.put("end_time", endTime.getTime());
		return map;
	}

	String questDelete(Input input) {
		Message msg = new Message(0);
		String id = input.getDesc("id", "");
		List list = QBuilder.db("basedb").select().from("Quest").where(new Where("id=", id)).query().getResultObj();
		if(list.size() == 0) {
			setErrcode(msg, NO_RESULT);
			return msg.toJson();
		}
		Integer ids = QBuilder.db("basedb").execHQL("update Quest set status = 0 where id =  " + id);
		return msg.toJson();
	}

	String commentDelete(Input input) {
		Message msg = new Message(0);
		String id = input.getDesc("id", "");
		List list = QBuilder.db("basedb").select().from("Comment").where(new Where("id=", id)).query().getResultObj();
		if(list.size() == 0) {
			setErrcode(msg, NO_RESULT);
			return msg.toJson();
		}
		Integer ids = QBuilder.db("basedb").execHQL("update Comment set status = 0 where id =  " + id);
		return msg.toJson();
	}
%>

